package com.aqiang.day0528_shop.main.view.view

import android.content.Context
import android.util.AttributeSet
import android.view.ViewGroup
import android.content.res.TypedArray
import com.aqiang.day0528_shop.R
import android.graphics.Rect
import android.view.View
import android.widget.LinearLayout


class LabelView @JvmOverloads constructor(var c:Context, var attrs:AttributeSet) : ViewGroup(c,attrs) {
    private var childHorizontalSpace:Int = 0
    private var childVerticalSpace:Int = 0
    init {
        val attrArray = context.obtainStyledAttributes(attrs, R.styleable.LabelView)
        if (attrArray != null) {
            childHorizontalSpace = attrArray.getDimensionPixelSize(R.styleable.LabelView_tabHorizontalSpace, 0)
            childVerticalSpace = attrArray.getDimensionPixelSize(R.styleable.LabelView_tabHorizontalSpace, 0)
            attrArray.recycle()
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)

        val widthModel = MeasureSpec.getMode(widthMeasureSpec)
        val heightModel = MeasureSpec.getMode(heightMeasureSpec)

        //左右边距
        val left = paddingLeft
        val top = paddingTop
        //记录每一行的宽度和高度
        var lineWidth = 0
        var lineHeight = 0
        // 如果是warp_content情况下，记录宽和高
        var containerWidth = 0
        var containerHeight = 0
        // 遍历每个子元素
        for (index in 0 until childCount) {
            val child = getChildAt(index)
            if (child.visibility === View.GONE) {
                continue
            }
            measureChild(child, widthMeasureSpec, heightMeasureSpec)
            val params = child.layoutParams

            val childWidth = child.measuredWidth + childHorizontalSpace
            val childHeight = child.measuredHeight + childVerticalSpace

            if (lineWidth + childWidth > widthSize - paddingRight - paddingLeft) {
                containerWidth = Math.max(lineWidth, childWidth)
                lineWidth = childWidth
                containerHeight += lineHeight
                lineHeight = childHeight
                val rect = Rect(
                    left,
                    containerHeight + top,
                    left + childWidth - childHorizontalSpace,
                    containerHeight + top + child.measuredHeight
                )
                child.tag = rect
            } else {
                val rect = Rect(
                    lineWidth + left,
                    containerHeight + top,
                    lineWidth + left + childWidth - childHorizontalSpace,
                    containerHeight + top + child.measuredHeight
                )
                child.tag = rect
                lineWidth += childWidth
                lineHeight = Math.max(lineHeight, childHeight)
            }
        }

        containerWidth = Math.max(containerWidth, lineWidth) + paddingLeft + paddingRight
        containerHeight += lineHeight + paddingTop + paddingBottom
        containerHeight -= childVerticalSpace
        setMeasuredDimension(
            if (widthModel == MeasureSpec.EXACTLY) widthSize else containerWidth,
            if (heightModel == MeasureSpec.EXACTLY) heightSize else containerHeight
        )
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        val childCount = childCount
        for (i in 0 until childCount) {
            val child = getChildAt(i)
            if (child.visibility == View.GONE) {
                continue
            }
            val location = child.tag as Rect
            child.layout(location.left, location.top, location.right, location.bottom)
        }
    }

}